"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const mobx_miniprogram_bindings_1 = require("mobx-miniprogram-bindings");
const index_1 = require("../../../store/index");
const order_1 = require("../../../services/order");
const statusMetaMap = {
    pending: { label: "待处理", desc: "厨师尚未接单" },
    accepted: { label: "已接收", desc: "厨师已确认订单" },
    rejected: { label: "已拒绝", desc: "订单已关闭" },
    cooking: { label: "制作中", desc: "美食正在出炉" },
    completed: { label: "已完成", desc: "期待你的评价" },
};
Page({
    data: {
        loading: true,
        order: null,
        statusLabel: "",
        statusDesc: "",
        funTotal: 0,
        createdText: "",
        acceptedText: "",
        rejectedText: "",
        startedText: "",
        finishedText: "",
        isChef: false,
        ratingOptions: [1, 2, 3, 4, 5],
    },
    storeBindings: null,
    onLoad(query) {
        var _a;
        this.storeBindings = (0, mobx_miniprogram_bindings_1.createStoreBindings)(this, {
            store: index_1.globalStore,
            fields: ["userProfile"],
        });
        const orderId = query.id || index_1.globalStore.activeOrderId;
        if (!orderId) {
            wx.showToast({ title: "订单不存在", icon: "none" });
            return;
        }
        this.setData({ isChef: ((_a = index_1.globalStore.userProfile) === null || _a === void 0 ? void 0 : _a.role) === "chef" });
        this.loadOrder(orderId);
    },
    onUnload() {
        var _a, _b;
        (_b = (_a = this.storeBindings) === null || _a === void 0 ? void 0 : _a.destroyStoreBindings) === null || _b === void 0 ? void 0 : _b.call(_a);
    },
    loadOrder(orderId) {
        return __awaiter(this, void 0, void 0, function* () {
            this.setData({ loading: true });
            try {
                const res = yield (0, order_1.getOrderDetail)(orderId);
                index_1.globalStore.updateOrderSnapshot(res.order);
                const funTotal = res.order.items.reduce((sum, item) => sum + item.funValue * item.quantity, 0);
                const statusMeta = statusMetaMap[res.order.status] || { label: res.order.status, desc: "" };
                this.setData({
                    order: res.order,
                    funTotal,
                    statusLabel: statusMeta.label,
                    statusDesc: statusMeta.desc,
                    createdText: this.formatTime(res.order.createdAt),
                    acceptedText: this.formatTime(res.order.acceptedAt),
                    rejectedText: this.formatTime(res.order.rejectedAt || res.order.closedAt),
                    startedText: this.formatTime(res.order.startedAt),
                    finishedText: this.formatTime(res.order.finishedAt || res.order.closedAt),
                });
            }
            catch (error) {
                console.error("加载订单详情失败", error);
                wx.showToast({ title: "加载失败", icon: "none" });
            }
            finally {
                this.setData({ loading: false });
            }
        });
    },
    formatTime(value) {
        if (!value)
            return "";
        return value.replace("T", " ").slice(0, 16);
    },
    handleRefresh() {
        var _a;
        const orderId = (_a = this.data.order) === null || _a === void 0 ? void 0 : _a._id;
        if (orderId) {
            this.loadOrder(orderId);
        }
    },
    handleBack() {
        wx.navigateBack({ delta: 1 });
    },
});
